<?php
/**
 * Zhub - A PHP Framework For API Developer
 *
 * This file is part of the Zhub package.
 *
 * (c) YiLong Fan <fantiq@163.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author fanyilong <fantiq@163.com>
 * @copyright 2017 fanyilong. All rights reserved.
 * @package Zhub
 * @date 18-2-27
 */

namespace Zhub\Prototype\Objective\Clazz\Constant;


use Zhub\DataStruct\Struct;
use Zhub\Prototype\Objective\Clazz\ClassUtils;
use Zhub\Prototype\Objective\Clazz\ClazzUtil;
use Zhub\Prototype\Objective\Clazz\Exceptions\EmptyNameException;

class ConstantStruct extends Struct
{
    const NAME = 'name';

    const DEFAULT_VALUE = 'defaultValue';

    public static function initialize(): array
    {
        return [
            self::NAME => '',
            self::DEFAULT_VALUE => null,
        ];
    }

    public function getName():string
    {
        return $this->get(self::NAME);
    }

    public function setName(string $name)
    {
        // class 不能作为常量
        ClazzUtil::checkConstName($name);
        $this->set(self::NAME, $name);
    }

    public function getDefaultValue()
    {
        return $this->get(self::DEFAULT_VALUE);
    }

    public function setDefaultValue($defaultValue)
    {
        $this->set(self::DEFAULT_VALUE, $defaultValue);
    }

    public function toString(): string
    {
        $name = $this->getName();
        if (empty($name)) {
            throw new EmptyNameException();
        }
        return 'const ' . $this->getName() . ' = ' . var_export($this->getDefaultValue(), true) . ';';
    }

}